package com.company.test18;

import com.company.test17.Test17_06;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Administrator on 2016.9.19.
 */
public class Test18_08 extends JFrame{
    class Ball extends JPanel{
        private int delay=10;

        private Timer timer=new Timer(delay,new TimerListener());

        private int x=0;
        private int y=5;
        private int dx=2;
        private int dy=2;

        public Ball(){
            timer.start();
        }

        private class TimerListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int radius=5;
            g.setColor(Color.BLUE);

            if (x<=radius) dx=Math.abs(dx);
            if (x>=getWidth()-radius) dx=-Math.abs(dx);
            if (y<=radius) dy=Math.abs(dy);
            if (y>=getHeight()-radius) dy=-Math.abs(dy);

            x+=dx;
            y+=dy;

            g.fillOval(x-radius,y-radius,radius,radius);
        }

        public void stop(){
            timer.start();
        }

        public void star(){
            timer.start();
        }

        public void setDelay(int delay){
            this.delay=delay;
            timer.setDelay(delay);
        }
    }

    class BallControl extends JPanel{
        private JScrollBar jsb=new JScrollBar(JScrollBar.HORIZONTAL);
        private JButton suspendJbt=new JButton("SuspendJbt");
        private JButton resumeJbt=new JButton("resumeJbt");
        private Ball ball=new Ball();

        public BallControl(){
            jsb.setMaximum(150);
            jsb.setMinimum(1);
            jsb.setVisibleAmount(50);

            this.setLayout(new BorderLayout());
            this.add(jsb,BorderLayout.NORTH);
            JPanel jPanel=new JPanel();
            jPanel.add(suspendJbt);
            jPanel.add(resumeJbt);
            this.add(jPanel,BorderLayout.SOUTH);
            this.add(ball,BorderLayout.CENTER);

            jsb.addAdjustmentListener(new AdjustmentListener() {
                @Override
                public void adjustmentValueChanged(AdjustmentEvent e) {
                    ball.setDelay(jsb.getValue());
                }
            });

            suspendJbt.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    super.mousePressed(e);
                    ball.stop();
                }
            });

            resumeJbt.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    super.mousePressed(e);
                    ball.star();
                }
            });
        }
    }

    public Test18_08(){
        add(new Ball());
        add(new BallControl());
    }

    public static void main(String[] args){
        JFrame frame=new Test18_08();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.setTitle("fire");
        frame.setVisible(true);
    }
}
